package gui.mainFrame.testConditionViewer.branchViewer;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import org.jgraph.JGraph;
import org.jgrapht.DirectedGraph;
import org.jgrapht.ext.JGraphModelAdapter;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.DefaultListenableGraph;
import org.jgrapht.graph.DirectedMultigraph;
import testGenerator.Controller;

/**
 *
 * @author William
 */
public class GraphPanel extends JPanel
{
    private static final long serialVersionUID = 1L;

    private ListenableDirectedMultigraph<String, DefaultEdge> g;
    private JGraphModelAdapter<String, DefaultEdge> jgAdapter;
    private JGraph jgraph;
    private final Controller controller;

    public GraphPanel(Controller controller)
    {
        this.controller = controller;
        reset();
    }

    private void reset()
    {
        // create a JGraphT graph
        g =
                new ListenableDirectedMultigraph<String, DefaultEdge>(
                DefaultEdge.class);

        // create a visualization using JGraph, via an adapter
        jgAdapter = new JGraphModelAdapter<String, DefaultEdge>(g);

        
        jgraph = new JGraph(jgAdapter);
    }

    void render()
    {
        reset();
        new BranchGraphManipulator(g, jgAdapter, controller);
        this.removeAll();
        this.setLayout(new BorderLayout());
        JScrollPane spane = new JScrollPane(jgraph);
        this.setSize(500, 500);

        this.add(spane, BorderLayout.CENTER);
        this.repaint();

    }

    /**
     * a listenable directed multigraph that allows loops and parallel edges.
     * @param <V>
     * @param <E>  
     */
    public static class ListenableDirectedMultigraph<V, E>
            extends DefaultListenableGraph<V, E>
            implements DirectedGraph<V, E>
    {

        private static final long serialVersionUID = 1L;

        ListenableDirectedMultigraph(Class<E> edgeClass)
        {
            super(new DirectedMultigraph<V, E>(edgeClass));
        }
    }
}
